package com.example.mail;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.R;
import core.model.Game;
import core.model.mail.Message;

import java.util.List;


public class MailListActivity extends Activity {

    private LinearLayout mailLineLayout;
    private Game game;
    private String selectedMailType;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maillinelayout);

        List<Message> messages = (List<Message>) this.getIntent().getExtras().getSerializable("messages");
        game = (Game) this.getIntent().getExtras().getSerializable("game");
        selectedMailType = this.getIntent().getExtras().getString("selectedMailType");

        mailLineLayout = (LinearLayout) findViewById(R.id.mailLine);

        LayoutInflater linflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

        // header
        View customHeaderView = linflater.inflate(R.layout.mailline, null);
        TextView turnHeader = (TextView) customHeaderView.findViewById(R.id.mailLineTurn);
        turnHeader.setText("Turn");

        TextView fromHeader = (TextView) customHeaderView.findViewById(R.id.mailLineFrom);
        fromHeader.setText("From");

        mailLineLayout.addView(customHeaderView);

        //body
        for (final Message message : messages) {
            final View customView = linflater.inflate(R.layout.mailline, null);
            TextView turn = (TextView) customView.findViewById(R.id.mailLineTurn);
            turn.setText(Integer.toString(message.getTurn()));

            TextView from = (TextView) customView.findViewById(R.id.mailLineFrom);
            from.setText(message.getFrom());

            customView.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Context context = mailLineLayout.getContext();
                    Intent i = new Intent(context, MailBodyActivity.class);

                    i.putExtra("turn", Integer.toString(message.getTurn()));
                    i.putExtra("from", message.getFrom());
                    i.putExtra("body", message.getBody());
                    i.putExtra("game", game);
                    i.putExtra("selectedMailType", selectedMailType);

                    context.startActivity(i);
                }
            });

            mailLineLayout.addView(customView);
        }

    }

}
